package com.yupi.yuapiinterface.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.yuapiinterface.mapper.ImageMapper;
import com.yupi.yuapiinterface.model.entity.Image;
import com.yupi.yuapiinterface.service.ImageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Random;

/**
 * @author <a href=""https://github.com/Fzs0>逢左使</a>
 * @create 2023/12/23 22:13
 * @Description:
 */
@Service
public class ImageServiceImpl extends ServiceImpl<ImageMapper, Image>
        implements ImageService {
    @Resource
    private ImageMapper imageMapper;

    @Override
    public String getImageUrl() {
        Random random = new Random();
        Image image = null;
        // 当获取到了图片才跳出循环
        while (image == null) {
            long id = random.nextInt(10) + 1;
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("id", id);
            image = imageMapper.selectOne(queryWrapper);
        }
        return image.getUrl();
    }
}
